<?php
require_once 'mp3\Smarty.php';

$smarty = new mp3_Smarty();

require_once 'mp3\GestionarUsuari.php';
require 'mp3/ComprovarSessio.php';


if (array_key_exists('confirmarRegistre', $_POST)){
	if ($_POST['pdp'] == $_POST['pdp_rep']) {
		$registre = new mp3_GestionarUsuari($_POST['usuari'],$_POST['pdp'],$_POST['mail']);
		$registre->registrar();
		if ($registre->ok()){
			//TODO: Redirect a formulari
			
			setcookie('userId', $registre->getUserId(), time()+3600);
			setcookie('userIdCrypt', sha1(Mp3_Constants::SALT.$registre->getUserId()), time()+3600);
			setcookie('userName', $registre->getUserName(), time()+3600);
			$_SESSION['userid'] = $registre->getUserId();
			$_SESSION['username'] = $registre->getUserName();
			require 'mp3/ComprovarSessio.php';
			echo $smarty->fetch('mp3\header.tpl');
			echo $smarty->fetch('mp3\pujarCanco.tpl');
			echo $smarty->fetch('mp3\footer.tpl');
		}else{
			$smarty->assign('missatge', $registre->getError());
			echo $smarty->fetch('mp3\header.tpl');
			echo $smarty->fetch('mp3\registre.tpl');
			echo $smarty->fetch('mp3\footer.tpl');
		}
	}else {
		$smarty->assign('missatge', 'Els paraulas de pas són diferents');
		echo $smarty->fetch('mp3\header.tpl');
		echo $smarty->fetch('mp3\registre.tpl');
		echo $smarty->fetch('mp3\footer.tpl');
	}
}else{
	$smarty->assign('missatge', '');
	echo $smarty->fetch('mp3\header.tpl');
	echo $smarty->fetch('mp3\registre.tpl');
	echo $smarty->fetch('mp3\footer.tpl');
}
?>
